package piece;
/*
 * Justin Prentice
 * TCSS 305 (winter quarter)
 * Tetris program - abstract piece
 */


/**
 * @author Justin Prentice
 * @version 1.1
 * This class represents a Tetris piece on a field represented in a Swing coordinate system.  
 */
public abstract class AbstractPiece implements Cloneable
{
  /** The x coordinate of the piece. */
  private int my_x;
  
  /** The y coordinate of the piece. */
  private int my_y;
  
  /** The current rotation of the shape. */
  private int my_state;
  
  /** An array of booleans representing all possible rotations. */
  private boolean[][][] my_rotations; //can't be final, or clone() won't work
  //boolean[rotation][row][column]
  
  /**
   * Constructs a piece at location x,y with a fixed set of rotations. 
   * @param the_x the x coordinate
   * @param the_y the y coordinate
   * @param the_rotations all possible rotations of the piece
   */
  public AbstractPiece(final int the_x, final int the_y, final boolean[][][] the_rotations) 
  {
    my_rotations = the_rotations.clone();
    my_x = the_x;
    my_y = the_y;
    my_state = 0;
  }
  
  /**
   * Moves the piece left one tile.
   */
  public void left()
  {
    my_x--;
  }
  
  /**
   * Moves the piece right one tile.
   */
  public void right()
  {
    my_x++;
  }
  
  /**
   * Moves the piece down one tile.
   */
  public void down()
  {
    my_y++;
  }
  
  /**
   * Rotates the piece clockwise 90 degrees.
   */
  public void rotate()
  {
    my_state++;
    if (my_state >= my_rotations.length)
    {
      my_state = 0;
    }
  }
  
  /**
   * Sets the x coordinate of the piece.
   * @param the_x the new x coordinate.
   */
  public void setX(final int the_x)
  {
    my_x = the_x;
  }
  
  /**
   * Returns the piece's x coordinate.
   * @return the x coordinate
   */
  public int getX()
  {
    return my_x;
  }
  
  /**
   * Sets the y coordinate of the piece.
   * @param the_y the new y coordinate.
   */
  public void setY(final int the_y)
  {
    my_y = the_y;
  }
  
  /**
   * Returns the piece's y coordinate.
   * @return the y coordinate
   */
  public int getY()
  {
    return my_y;
  }
  /**
   * Returns an array representing the layout of the piece.
   * @return a 2d array of booleans representing the layout of the piece
   */
  public boolean[][] getState()
  {
    return my_rotations[my_state].clone();
  }
  /**
   * {@inheritDoc}
   */
  @Override
  public String toString()
  {
    final StringBuilder sb = new StringBuilder(getClass().getName() + ":\n");
    for (int i = 0; i < my_rotations[my_state].length; i++)
    {
      for (int j = 0; j < my_rotations[my_state][i].length; j++)
      {
        if (my_rotations[my_state][i][j]) 
        {
          sb.append("x");
        }
        else
        {
          sb.append(" ");
        }
      }
      sb.append("\n");
    }
    return sb.toString();
    
  }
  
  /**
   * {@inheritDoc}
   */
  @Override
  public AbstractPiece clone() throws CloneNotSupportedException
  {
    //modeled after fruit example on java practices
    final AbstractPiece result = (AbstractPiece) super.clone();
    result.my_rotations = my_rotations.clone();
    return result;
  }

  /**
   * translates the piece by x,y.
   * @param the_x the x translation
   * @param the_y the y translation
   */
  public void move(int the_x, int the_y)
  {
    my_x += the_x;
    my_y += the_y;
  }
}
